using JetBrains.Annotations;
using Voodoo.Analytics;
using Voodoo.Sauce.Common.Utils;
using Voodoo.Sauce.Core;
using Voodoo.Sauce.Internal.Extension;
using Voodoo.Sauce.Internal.Utils;
using Voodoo.Sauce.Privacy;

namespace Voodoo.Sauce.Internal.Analytics
{
	public class VoodooAnalyticsEventParameters : VanEventBasicParameters
	{
		private const string FAKE_VALUE = "fake";

		private static readonly PrivacyCore Privacy = VoodooSauceCore.GetPrivacy();

		[CanBeNull]
		private string _vsVersion;

		[CanBeNull]
		private string _appBundleId;

		[CanBeNull]
		private string _appVersion;

		[CanBeNull]
		private string _platform;

		[CanBeNull]
		private string _developerDeviceId;

		[CanBeNull]
		private string _firstAppLaunchDate;

		[CanBeNull]
		private string _mediation;

		[CanBeNull]
		private string _segmentationUuid;

		[CanBeNull]
		private string _abTestUuid;

		[CanBeNull]
		private string _abTestCohortUuid;

		[CanBeNull]
		private string _abTestVersionUuid;

		[CanBeNull]
		private AttributionData _attributionData;

		internal override void SetVsVersion(string vsVersion)
		{
			_vsVersion = vsVersion;
		}

		internal override void SetAppVersion(string appVersion)
		{
			_appVersion = appVersion;
		}

		internal override void SetAppBundleId(string appBundleId)
		{
			_appBundleId = appBundleId;
		}

		internal override void SetMediation(string mediation)
		{
			_mediation = mediation;
		}

		internal override void SetSegmentationUuid(string segmentationUuid)
		{
			_segmentationUuid = segmentationUuid;
		}

		internal override void SetAbTestUuid(string abTestUuid)
		{
			_abTestUuid = abTestUuid;
		}

		internal override void SetAbTestCohortUuid(string abTestCohortUuid)
		{
			_abTestCohortUuid = abTestCohortUuid;
		}

		internal override void SetAbTestVersionUuid(string abTestVersionUuid)
		{
			_abTestVersionUuid = abTestVersionUuid;
		}

		public override string GetPlatform()
		{
			if (_platform != null)
			{
				return _platform;
			}
			if (PlatformUtils.UNITY_EDITOR)
			{
				_platform = "editor";
			}
			else if (PlatformUtils.UNITY_IOS)
			{
				_platform = "ios";
			}
			else if (PlatformUtils.UNITY_ANDROID)
			{
				_platform = "android";
			}
			return _platform;
		}

		public override string GetVSVersion()
		{
			return _vsVersion;
		}

		public override string GetAppBundleId()
		{
			return _appBundleId;
		}

		public override string GetAppVersion()
		{
			return _appVersion;
		}

		public override string GetScreenResolution()
		{
			return DeviceUtils.GetResolution();
		}

		public override string GetOSVersion()
		{
			return DeviceUtils.OperatingSystemVersion;
		}

		public override string GetManufacturer()
		{
			return DeviceUtils.Manufacturer;
		}

		public override string GetModel()
		{
			return DeviceUtils.Model;
		}

		public override string GetUserId()
		{
			return AnalyticsUserIdHelper.GetUserId();
		}

		public override string GetLocale()
		{
			return DeviceUtils.GetLocale();
		}

		public override string GetConnectivity()
		{
			return DeviceUtils.GetConnectivity();
		}

		public override int GetAppLaunchCount()
		{
			return AnalyticsStorageHelper.Instance.GetAppLaunchCount();
		}

		public override int GetGameCount()
		{
			return AnalyticsStorageHelper.Instance.GetGameCount();
		}

		public override string GetDeveloperDeviceId()
		{
			if (_developerDeviceId != null)
			{
				return _developerDeviceId;
			}
			_developerDeviceId = Privacy.GetVendorId().Replace("-", "").ToLower();
			return _developerDeviceId;
		}

		public override bool GetAdsConsentGiven()
		{
			return Privacy.HasAdsConsent();
		}

		public override bool GetAnalyticsConsentGiven()
		{
			return Privacy.HasAnalyticsConsent();
		}

		public override string GetAdvertisingId()
		{
			return Privacy.GetAdvertisingId();
		}

		public override bool HasLimitAdTrackingEnabled()
		{
			if (!PlatformUtils.UNITY_EDITOR)
			{
				return Privacy.HasLimitAdTrackingEnabled();
			}
			return true;
		}

		public override string GetIdfaAuthorizationStatus()
		{
			if (!Privacy.HasLimitAdTrackingEnabled())
			{
				return IdfaAuthorizationStatus.Authorized.ToString();
			}
			return IdfaAuthorizationStatus.Denied.ToString();
		}

		public override string GetInstallStore()
		{
			return DeviceUtils.GetAndroidInstallStore();
		}

		public override string GetFirstAppLaunchDate()
		{
			if (_firstAppLaunchDate != null)
			{
				return _firstAppLaunchDate;
			}
			_firstAppLaunchDate = AnalyticsStorageHelper.Instance.GetFirstAppLaunchDate()?.ToIsoFormat();
			return _firstAppLaunchDate;
		}

		public override string GetMediation()
		{
			return _mediation;
		}

		public override string GetSegmentationUuid()
		{
			return _segmentationUuid;
		}

		public override string GetAbTestUuid()
		{
			return _abTestUuid;
		}

		public override string GetAbTestCohortUuid()
		{
			return _abTestCohortUuid;
		}

		public override string GetAbTestVersionUuid()
		{
			return _abTestVersionUuid;
		}

		public override string GetAttributionDataName()
		{
			if (PlatformUtils.UNITY_EDITOR)
			{
				return "fake";
			}
			return _attributionData?.Name;
		}

		public override string GetCurrentLevel()
		{
			string currentLevel = AnalyticsStorageHelper.Instance.GetCurrentLevel();
			if (!string.IsNullOrEmpty(currentLevel))
			{
				return currentLevel;
			}
			return null;
		}
	}
}
